//
/* 
 @from http://en.wikipedia.org/wiki/Continued_fraction
 
 h(n) = a(n)*h(n-1) + h(n-2)
 k(n) = a(n)*k(n-1) + k(n-2)
 
 and 
 h(0) = a(0), k(0) = 1;
 h(1) = a(0)*a(1) + 1, k(1) = a(1);
*/

//preloading.....
load("library/common.js");

JEuler.system.loadJS("library/math.js");
JEuler.system.loadJS("library/bigInteger.js");

(function() {
    
    var BigInteger = JEuler.math.BigInteger;
    
    function a(n) {
        if(n==0) return 1;
        return 2;
    }

    //JEuler.system.loadJS("data/???.dat");
    JEuler.system.printCaption("57");
    
    JEuler.system.startTimerCount();
    
    var h=[], k=[], sum=0;
    h[0] = new BigInteger(a(0)); 
    k[0] = new BigInteger(1);
    h[1] = new BigInteger(a(0)*a(1) + 1); 
    k[1] = new BigInteger(a(1));
    
    JEuler.system.print("h("+(1)+")=" + h[0]);
    JEuler.system.print("h("+(2)+")=" + h[1]);
    for(var n=2; n<=1000; n++) {
        h[n] = h[n-1].clone().times(a(n)).add(h[n-2]);
        k[n] = k[n-1].clone().times(a(n)).add(k[n-2]);
        
        if(h[n].getDigitCount()>k[n].getDigitCount()) {
            sum++;
            JEuler.system.print(h[n].getDigitCount() + "," + k[n].getDigitCount());
        }
    }    
    
    JEuler.system.print("Sum = " + sum);

    JEuler.system.printTimerCount();
    
})();
